//
//  CircleAvatar.swift
//  MovieDemo
//
//  Created by linhan on 15/6/26.
//  Copyright (c) 2015年 linhan. All rights reserved.
//

import Foundation
import UIKit
class CircleAvatar: UIView
{
    private var _radius:CGFloat = 0
    private var _imageView:ImageLoader = ImageLoader()
    private var _borderColor:UIColor = UIColor.white
    private var _borderWidth:CGFloat = 0
    
    
    //内圆颜色
    var innerColor:UIColor?
    {
        didSet
        {
            _imageView.backgroundColor = innerColor
        }
    }
    
    var image:Any?
    {
        didSet
        {
            if let url = image as? String
            {
                //_imageView.load(url, placeholderImage: SWThemeStyle.randomPlaceholderCircleImage())
                _imageView.load(url)
            }
            else if let imageObj = image as? UIImage
            {
                let roundImage = Toucan.Mask.maskImageWithRoundedRect(imageObj, cornerRadius: imageObj.size.width * 0.5)
                _imageView.image = roundImage
            }
        }
    }
    
    var imageView:ImageLoader
    {
        return _imageView
    }
    
    override var contentMode:UIViewContentMode
    {
        get
        {
            return _imageView.contentMode
        }
        set
        {
            _imageView.contentMode = newValue
        }
    }
    
    init(radius:CGFloat, borderWidth:CGFloat = 0, borderColor:UIColor = UIColor.white)
    {
        _radius = radius
        let rect:CGRect = CGRectMake(0, 0, _radius * 2, _radius * 2)
        super.init(frame: rect)
        
        //layer.cornerRadius = radius
        //layer.masksToBounds = true
        
        //_imageView.layer.masksToBounds = true
        _imageView.frame = bounds
        _imageView.contentMode = .scaleAspectFill
        _imageView.options.cornerRadius = radius
        _imageView.options.borderWidth = borderWidth
        _imageView.options.borderColor = borderColor
        addSubview(_imageView)
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
}
